qui(
    function(require, exports, module) {

        Sginal.Grid = function(options) {
            this.dao = Sginal.Dao;
            this.config = Sginal.Config;
            this.util = Sginal.Util;
            this.options = options || {};

			//this.channel_id = Com.url.getQueryValue("channel_id",location.href);
			//this.channel_name = Com.url.getQueryValue("channel_name",location.href);
            this.construct = options.construct || function() {
                this.init();
            };
            this.construct.call(this);
        };

        Com.inherits(Sginal.Grid, Ancestor.Grid).extend({
            init: function() {
                var self = this;
                var $container = self.options.$container;
                self.$grid = $container.find('table.grid');
                self.perpareData();
                self.bindBasicEvent();
                self.initSuggest();
                self.query();
            },

            perpareData: function() {
                var self = this;
                var username = $("div.user a.name").html();
            },

            bindBasicEvent: function() {
                var self = this;

                var $container = self.options.$container;

                $container.find('.query-add').click(function() {
					window.open("/epg/sginal_edit.html");
                });
                

                $container.find('.query-btn').on('click.query', function() {
                    $('input[name=pageNo]').val(1);
                    self.query();
                    return false;
                });

                $container.find('.pageClick').on('click.query', function() {
                    self.query();
                    return false;
                });

                $container.find('.server-home').click(function() {
                    window.open("/epg/server_home.html");
                });
			},

			bindFormEvent: function($form) {
                var self = this;

			},

            run: function(options, callback) {
                var self = this;
                var colModelConfig = Sginal.Config.TABLE_COLMODEL_CONFIG;

                var tableConfig = $.extend(true, {}, Sginal.Config.TABLE_STATUS );
                tableConfig.width =  875;
                var colModel = tableConfig.colModel;
                var newColModel = [];

                $.each(colModel, function(i, model) {
                    newColModel.push(colModelConfig[model]);
                });
                tableConfig.colModel = newColModel;
                //$('#statusList').jqGrid(tableConfig);

                tableConfig = $.extend(true, {}, Sginal.Config.TABLE_LOG );
                tableConfig.width =  875;
                var colModel = tableConfig.colModel;
                var newColModel = [];

                $.each(colModel, function(i, model) {
                    newColModel.push(colModelConfig[model]);
                });
                tableConfig.colModel = newColModel;
                $('#sginalLogList').jqGrid(tableConfig);
            },

            getParam: function() {
                var self = this;
                var $container = self.options.$container;

                var $grid = self.$grid;
                var params = {};
                var $form = $container.find('form[name="basicFrm"]');

                var $elements = $.makeArray($form.prop('elements')).filter(function(item) {
					return true;
                });

				var checkboxVal = [];
                var name;
                var key;
                $.each($elements, function() {
                    name = this.name;
                    if (name) {
                        if($(this).val()){
                            params[name] = $.trim($(this).val());
                        }
                    }

                });
                var id = Com.url.getQueryValue("id",location.href);
                if (id != null) {
                    params["id"] = id;
                    id = null;
                }
                return params;
            },

            query: function() {
                var self = this;

                var params = self.getParam();
                var pn = parseInt(params['pageNo']);
                Sginal.Dao.getList(params, function(data){
                    if (data != null && data.code=="A00000" && data.data.result != null) {
                        var sginals = data.data.result;
                        var gridDatas = "";
                        $.each(sginals, function (n, sginal) {
                            gridDatas += '<div class="MenuPanel row "><div class="MenuTitle"><ul>';
                            gridDatas += '<li class="smallC slide">' + sginal.id + '</li>';
                            gridDatas += '<li class="bigC slide">' + sginal.name + '</li>';
                            gridDatas += '<li class="smallC slide">' + sginal.port + '</li>';
                            gridDatas += '<li class="smallC slide">' + Sginal.Util.formatter.sginalStatus(sginal.status, '') + '</li>';
                            gridDatas += '<li class="smallC slide">' + sginal.author + '</li>';
                            gridDatas += '<li class="largeC slide" style="width: 20%">' + Sginal.Util.formatter.operate('', sginal) + '</li></ul></div>';
                            gridDatas += '<div class="MenuContent"><table class="table table-hover"><thead><tr>';
                            gridDatas += '<th>接入服务器IP<th>';
                            gridDatas += '<th>信号输入类型<th>';
                            gridDatas += '<th>信号输入IP<th>';
                            gridDatas += '<th>信号输出类型<th>';
                            gridDatas += '<th>信号输出IP<th>';
                            gridDatas += '<th>网卡号<th>';
                            gridDatas += '<th>服务器类型<th></tr></thead><tbody>';
                            $.each(sginal.sginalServers, function (n, sginalServer) {
                                gridDatas += '<tr>';
                                gridDatas += '<td>' + sginalServer.interfaceIp + '<td>';
                                gridDatas += '<td>' + Sginal.Util.formatter.sginalType(sginalServer.inputType, '') + '<td>';
                                gridDatas += '<td>' + sginalServer.inputIp + '<td>';
                                gridDatas += '<td>' + Sginal.Util.formatter.sginalType(sginalServer.outputType, '') + '<td>';
                                gridDatas += '<td>' + sginalServer.outputIp + '<td>';
                                if (typeof(sginalServer.ethNum)!='undefined') {
                                    gridDatas += '<td>' + sginalServer.ethNum + '<td>';
                                } else {
                                    gridDatas += '<td>无<td>';
                                }
                                gridDatas += '<td>' + Sginal.Util.formatter.serverType(sginalServer.serverType, '') + '<td>';
                                gridDatas += '<td><a target="_blank" href="signal_status.html?fatherId='+sginal.id+'&serverIp='+sginalServer.interfaceIp+'&name='+encodeURI(sginal.name)+'">状态</a><td>';
                                gridDatas += '</tr>';
                            });
                            gridDatas += '</tbody></table></div></div>';
                        });
                        $('.panel-body').html(gridDatas);

                        var dataCount=data.data.totalCount; //总数据条数
                          var pageSize=data.data.pageSize;//每页显示条数
                          var pageCount= Math.ceil(dataCount / pageSize);//总页数
                          if(pn==0||pn>pageCount){
                            return;
                          }
                          var ul=$(".listul");
                          ul.empty();
                          self.paintPage(pageCount,pn);   //绘制页码

                        $(".MenuPanel").each(function(){
                            $(this).children(".MenuContent").hide();
                        });

                        $(".MenuTitle ul .slide").each(function(){
                            $(this).click(function(){

                                if($(this).parents(".MenuPanel").children(".MenuContent").css("display") != "none"){
                                   $(this).parents(".MenuPanel").children(".MenuContent").slideUp("fast");
                                }else{
                                    $(this).parents(".MenuPanel").children(".MenuContent").slideDown("fast");
                                }
                            });

                        });
                        return true;
                    }
                });
            },

            paintPage: function(number, currNum) {//number 总页数,currNum 当前页
                var self = this;
                var pageUl=$(".fenye");
                    pageUl.empty();
                    var ulDetail="";

                    if(number==1){
                     ulDetail= "<li class=\"prev\"><a href=\"javascript:void(0)\">上一页</a></li>"+
                     "<li class=\"numb choose\"><a href='javascript:void(0);' onclick='Sginal.Util.getList(1)'>1</a></li>"+
                     "<li class=\"next\"><a href=\"javascript:void(0)\">下一页</a></li>";
                   }else if(number==2){
                     ulDetail= "<li class=\"prev\"><a href='javascript:void(0);' onclick='Sginal.Util.getList(1)'>上一页</a></li>"+
                     "<li class=\"numb"+self.choosele(currNum,1)+"\"><a href='javascript:void(0);' onclick='Sginal.Util.getList(1)'>1</a></li>"+
                     "<li class=\"numb"+self.choosele(currNum,2)+"\"><a href='javascript:void(0);' onclick='Sginal.Util.getList(2)'>2</a></li>"+
                     "<li class=\"next\"><a href='javascript:void(0);' onclick='Sginal.Util.getList(2)'>下一页</a></li>";
                   }else if(number==3){
                     ulDetail= "<li class=\"prev\"><a href='javascript:void(0);' onclick='Sginal.Util.getList(" + parseInt(currNum-1) + ")'>上一页</a></li>"+
                     "<li class=\"numb"+self.choosele(currNum,1)+"\"><a href='javascript:void(0);' onclick='Sginal.Util.getList(1)'>1</a></li>"+
                     "<li class=\"numb"+self.choosele(currNum,2)+"\"><a href='javascript:void(0);' onclick='Sginal.Util.getList(2)'>2</a></li>"+
                     "<li class=\"numb"+self.choosele(currNum,3)+"\"><a href='javascript:void(0);' onclick='Sginal.Util.getList(3)'>3</a></li>"+
                     "<li class=\"next\"><a href='javascript:void(0);' onclick='Sginal.Util.getList(" + parseInt(currNum+1) + ")'>下一页</a></li>";
                   }else if(number==currNum&&currNum>3){
                     ulDetail= "<li class=\"prev\"><a href='javascript:void(0);' onclick='Sginal.Util.getList(" + parseInt(currNum-1) + ")'>上一页</a></li>"+
                     "<li class=\"numb\"><a href='javascript:void(0);' onclick='Sginal.Util.getList(" + parseInt(currNum-2) + ")'>"+parseInt(currNum-2)+"</a></li>"+
                     "<li class=\"numb\"><a href='javascript:void(0);' onclick='Sginal.Util.getList(" + parseInt(currNum-1) + ")'>"+parseInt(currNum-1)+"</a></li>"+
                     "<li class=\"numb choose\"><a href='javascript:void(0);' onclick='Sginal.Util.getList(" + currNum + ")'>"+currNum+"</a></li>"+
                     "<li class=\"next\"><a href='javascript:void(0);' onclick='Sginal.Util.getList(" + currNum + ")'>下一页</a></li>";
                   }else if(currNum==1&&number>3){
                     ulDetail= "<li class=\"prev\"><a href=\"javascript:void(0)\">上一页</a></li>"+
                     "<li class=\"numb choose\"><a href=\"javascript:void(0)\">1</a></li>"+
                     "<li class=\"numb\"><a href='javascript:void(0);' onclick='Sginal.Util.getList(2)'>2</a></li>"+
                     "<li class=\"numb\"><a href='javascript:void(0);' onclick='Sginal.Util.getList(3)'>3</a></li>"+
                     "<li class=\"next\"><a href='javascript:void(0);' onclick='Sginal.Util.getList(2)'>下一页</a></li>";
                   }else{
                     ulDetail= "<li class=\"prev\"><a href='javascript:void(0);' onclick='Sginal.Util.getList(" + parseInt(currNum-1) + ")'>上一页</a></li>"+
                     "<li class=\"numb\"><a href='javascript:void(0);' onclick='Sginal.Util.getList(" + parseInt(currNum-1) + ")'>"+parseInt(currNum-1)+"</a></li>"+
                     "<li class=\"numb choose\"><a href='javascript:void(0);' onclick='Sginal.Util.getList(" + currNum + ")'>"+currNum+"</a></li>"+
                     "<li class=\"numb\"><a href='javascript:void(0);' onclick='Sginal.Util.getList(" + parseInt(currNum+1) + ")'>"+parseInt(currNum+1)+"</a></li>"+
                     "<li class=\"next\"><a href='javascript:void(0);' onclick='Sginal.Util.getList(" + parseInt(currNum+1) + ")'>下一页</a></li>";
                   }

                   $(".fenye").append(ulDetail);
            },

            choosele: function(num,cur) {
                    if(num==cur){
                        return " choose";
                    }else{
                        return "";
                    }
            },


            initSuggest: function() {
                var serverIpSuggest = {
                    options: {
                        source: "/api/server/list",
                        method: "post",
                        multiple: false,
                        parseValue: function () {
                            var val = this.element.val(),
                                ret = []
                            return ret;
                        },
                        getParam: function (term) {
                            var requestData = {serverIp: term,status: 1,server_type: 2, timeSign: new Date().getTime()};
                            return requestData;
                        },

                        process: function (json) {
                            var ret = [],
                                self = this,
                                options = self.options,
                                itemData = null;
                            var data = null;
                            if (json == null) {
                                return [];
                            }
                            if (json.code === "A00000") {
                                data = $.isArray(json) ? json : json.data.result;
                            }
                            if (!data) {
                                return [];
                            }
                            $.map(data, function (item) {
                                itemData = {};
                                itemData[options.valueKey] = item["id"];
                                itemData[options.labelKey] = item["serverIp"];
                                itemData["serverIp"] = item["serverIp"];
                                itemData["serverPort"] = item["serverPort"];
                                itemData["province"] = item["province"] || "";
                                itemData["city"] = item["city"] || "";
                                itemData["remark"] = item["remark"];
                                ret.push(itemData);
                            });
                            return ret;
                        },

                        template: function (item) {
                            var formatArr = [];
                            formatArr.push('<span class="contract-width100">' + item["serverIp"] + '</span>');
                            formatArr.push('<span class="contract-width100">' + item["serverPort"] + '</span>');
                            formatArr.push('<span class="contract-width80">' + item["province"] + '</span>');
                            formatArr.push('<span class="contract-width80">' + item["city"] + '</span>');
                            formatArr.push('<span class="contract-width300">' + item["remark"] + '</span>');
                            return '<div class="contract-item" >' + formatArr.join("") + '</div>';
                        },
                        header: '<div class="contract-item" >' +
                            '<span class="contract-width100">服务器IP</span>' +
                            '<span class="contract-width100">服务器端口号</span>' +
                            '<span class="contract-width80">省份</span>' +
                            '<span class="contract-width80">市区</span>' +
                            '<span class="contract-width300">描述</span>'
                    }
                };

                //$("input[name=fgIp]").suggest(serverIpSuggest.options);
                //$("input[name=idcIp]").suggest(serverIpSuggest.options);
            }
        });

    }
);